home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_09 / allison / dump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-10  |  1.5 KB  |  71 lines

  1. /* dump.c:   Display a file's bytes in hex and ASCII */
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5.  
  6. /* Number of bytes per line */
  7. #define NBYTES 16
  8.  
  9. void dump(FILE *, char *);
  10.  
  11. main(int argc, char *argv[])
  12. {
  13.     FILE *f;
  14.     int i;
  15.  
  16.     /* Process files on command-line */
  17.     for (i = 1; i < argc; ++i)
  18.         if ((f = fopen(argv[i],"rb")) == NULL)
  19.             fprintf(stderr,"Can't open %s\n");
  20.         else
  21.         {
  22.             dump(f,argv[i]);
  23.             fclose(f);
  24.             putchar('\f');
  25.         }
  26.  
  27.     return 0;
  28. }
  29.  
  30. void dump(FILE *f, char *s)
  31. {
  32.     unsigned char buf[NBYTES];
  33.     int count;
  34.     long size = 0L;
  35.  
  36.     printf("Dump of %s:\n\n",s);
  37.     while ((count = fread(buf,1,NBYTES,f)) > 0)
  38.     {
  39.         int i;
  40.  
  41.         /* Print byte counter */
  42.         printf("  %06X ",size += count);
  43.  
  44.         /* Print Hex Bytes */
  45.         for (i = 0; i < NBYTES; ++i)
  46.         {
  47.             /* Print gutter space between columns */
  48.             if (i == NBYTES / 2)
  49.                 putchar(' ');
  50.  
  51.             /* Display hex byte */
  52.             if (i < count)
  53.             {
  54.                 printf(" %02X",buf[i]);
  55.                 if (!isprint(buf[i]))
  56.                     buf[i] = '.';
  57.             }
  58.             else
  59.             {
  60.                 /* Spacing for partial last line */
  61.                 fputs("   ",stdout);
  62.                 buf[i] = ' ';
  63.             }
  64.         }
  65.  
  66.         /* Print Text Bytes */
  67.         printf(" |%16.16s|\n",buf);
  68.     }
  69. }
  70.  
  71.